<template>
    <div class="login-container">
        <div class="left">
        <div class="title-box">
        <img src="../../assets/img/login_icon.png" alt="" />
        <span class="title">黑马面面</span><span class="line"></span>
        <span class="sub-title">用户登录</span>
        </div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" width="100%" class="login-Form">
        <el-form-item prop="phone">
        <el-input  v-model="ruleForm.phone" prefix-icon="el-icon-user" placeholder="请输入手机号"></el-input>
        </el-form-item>
         <el-form-item prop="password">
        <el-input type="password" v-model="ruleForm.password" prefix-icon="el-icon-lock" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <el-row>
           <el-col :span="16">
         <el-form-item prop="code">
        <el-input placeholder="请输入验证码" prefix-icon="el-icon-key" v-model="ruleForm.code" ></el-input>
      </el-form-item>
    </el-col>
    <el-col :span="7" style="margin-left:10px;">
    <img :src="loginCaptcha" alt="" @click="getLoginCaptcha" style="width:100%;height:40px">
    </el-col>
        </el-row>
        <el-form-item prop="checked">
        <el-checkbox v-model="ruleForm.checked"></el-checkbox> 我已阅读并同意<el-link type="primary" href='https://www.baidu.com/' target="_blank">用户协议</el-link>和
        <el-link type="primary" href='https://www.baidu.com/' target="_blank">隐私条款</el-link>
        </el-form-item>
        <el-form-item >
        <el-button type="primary" style="width:100%" @click="login">登陆</el-button>
        </el-form-item>
        <el-form-item >
        <el-button type="primary" style="width:100%" @click="register">注册</el-button>
        </el-form-item>
       </el-form>
        </div>
        <div class="right">
          <img src="../../assets/img/login_bg.png" alt="">
        </div>
        <register ref="register"></register>
    </div>
</template>

<script>
// 按需导入
import { saveToken } from '@/utils/token.js'
// 导入子组件
import register from '../login/register'
export default {
  name: 'login',
  // 注册子组件
  components: {
    register
  },
  data () {
    return {
      loginCaptcha: `${process.env.VUE_APP_BASEURL}/captcha?type=login`,
      ruleForm: {
        phone: '18511111111',
        password: '12345678',
        code: '',
        checked: true
      },
      rules: {
        phone: [{
          // rule:规则
          // value:值
          // callback回调函数,正确返回callback,错误callback返回错误信息
          // validator约定的校验规则
          validator: (rule, value, callback) => {
            if (!value) return callback(new Error('手机号不能为空'))
            const reg = /^1[3456789][0-9]{9}$/
            if (!reg.test(value)) return callback(new Error('手机号不合法'))
            callback()
          },
          trigger: 'blur'
        }],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        checked: [{
          validator: (rule, value, callback) => {
            value ? callback() : callback(new Error('协议必须选中'))
          },
          trigger: 'change'
        }]
      }
    }
  },
  methods: {
    getLoginCaptcha () {
      this.loginCaptcha = `${process.env.VUE_APP_BASEURL}/captcha?type=login&uuu=${new Date() - 0}`
    },
    login () {
    // 首先全局校验
    // async和await是一对,必须成对出现,哪个函数调用await,axync就出现哪个函数前面
    // 不使用promise是为了避免形成回调地狱
    // 返回的值太长我们通过在request.js里设置响应拦截返回我们需要的数据,res.data
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return
        const res = await this.$axios.post('/login', this.ruleForm)
        // console.log(res)
        if (res.code === 200) {
          // message.success弹出框成功的样式
          this.$message.success('登陆成功')
          // 保存token到本地
          // 抽取token
          // localStorage.setItem('heimamm64', res.data.token)
          saveToken(res.data.token)

          // 跳转页面
          this.$router.push('/layout')
        } else {
          this.$message.error(res.message)
          // 刷新验证码
          this.getLoginCaptcha()
        }
      })
    },
    register () {
      // f父子传值修改
      this.$refs.register.dialogVisible = true
    }
  }
}
</script>

<style lang="less">
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  //   右边盒子
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    //   头部盒子
    .title-box {
      display: flex;
      align-items: center;
      //   图片
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      // 黑马面面
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      // 分格线
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      // 用户登录
      .sub-title {
        font-size: 22px;
      }
    }
  }
  .login-Form{
    margin-top: 29px;
    display: block;
 }
}

</style>
